/**
 * 获取首页列表
 */
function getIndexList() {

    /**
     *  indexList
     */
    proxy(config.vfs_playlist, function (res) {
        var response = JSON.parse(res);
        var row = response.rows;
        var html = '';
        for (var i = 0; i < 2; i++) {
            var list = row[i];
            html += '<div class="col-lg-6 col-sm-6 col-xs-6">'
                + '<div class="full-content relative">'
                + '<div class="recomend-pic relative">'
                + '<a href="video.html?playlist_id=' + list.playlistid + '"><img src="' + list.pic + '"/></a>'
                + '<span class="absolute score">' + list.doubanscore + '</span>'
                + '</div>'
                + '<div class="recomend-name">'
                + list.name
                + '</div>'
                + '</div>'
                + '</div>'
        }
        document.getElementById('indexList').innerHTML = html;
        console.log(row)
    });


    /**
     *  SelectedList
     */
    proxy(config.vfs_playlist, function (res) {
        var response = JSON.parse(res);
        var row = response.rows;
        var html = '';
        for (var i = 2; i < 5; i++) {
            var list = row[i];
            html += '<div class="col-lg-4 col-sm-4 col-xs-4">'
                + '<div class="full-content relative">'
                + '<div class="choiceness-pic relative">'
                + '<a href="video.html?playlist_id=' + list.playlistid + '"><img src="' + list.pic + '"/></a>'
                + '<span class="absolute score">' + list.doubanscore + '</span>'
                + '</div>'
                + '<div class="recomend-name">'
                + list.name
                + '</div>'
                + '</div>'
                + '</div>'
        }
        document.getElementById('SelectedList').innerHTML = html
    });


    /**
     *  ActionList
     */
    proxy(config.vfs_playlist, function (res) {
        var response = JSON.parse(res);
        var row = response.rows;
        var html = '';
        for (var i = 1; i < 4; i++) {
            var list = row[i];
            html += '<div class="col-lg-4 col-sm-4 col-xs-4">'
                + '<div class="full-content relative">'
                + '<div class="choiceness-pic relative">'
                + '<a href="video.html?playlist_id=' + list.playlistid + '"><img src="' + list.pic + '"/></a>'
                + '<span class="absolute score">' + list.doubanscore + '</span>'
                + '</div>'
                + '<div class="recomend-name">'
                + list.name
                + '</div>'
                + '</div>'
                + '</div>'
        }
        document.getElementById('ActionList').innerHTML = html
    });
}

/**
 * 获取内页信息
 */
function getVideoInfo() {

    //获取playlist_id 形成真实url地址
    var _url = location.href;
    var position = _url.lastIndexOf('=');
    var playlist_id = _url.substring(position + 1, _url.length);

    var url = config.vfs_video.replace('%s', playlist_id);

    //使用proxy 调取内页数据
    proxy(url, function (res) {
        var response = JSON.parse(res);
        var playlist_info = response.playlistinfo;
        var html = '';
        var html_instructor = '';
        html += html += '<div class="col-lg-5 col-sm-5 col-xs-5">'
            + '<div class="full-content relative video-img">'
            + '<img src="' + playlist_info.pic + '"/>'
            + '</div>'
            + '</div>'

            + '<div class="col-lg-7 col-sm-7 col-xs-7 video-n-pad">'
            + '<div class="video-box relative">'
            + '<h4 class="video-name">' + playlist_info.name + '</h4>'
            + '<ul class="video-infos relative">'
            + '<li>导演：' + playlist_info.director + '</li>'
            + '<li>主演：' + playlist_info.playactor + '</li>'
            + '<li>' + playlist_info.area + '</li>'
            + '<li>2016-03-04大陆上映</li>'
            + '</ul>'
            + '<div class="absolute video-score"><i class="fa fa-star-o"></i> <span>评分：9.5</span></div>'

            + '</div>'
            + '</div>'
        document.getElementById('videoInfo').innerHTML = html;


        var html_instructor = playlist_info.introduction;

        document.getElementById('videoInstructor').innerHTML = html_instructor;
        console.log(response)
    });

    //数据获取之后, 替换当前url中的video 为payment 形成url jump paymet页面
    var _purl = _url.replace('video', 'payment');
    document.getElementById('downloading').href = _purl;

}


/**
 * 获取下载任务, 用于检测设备是否在线和下载任务
 * @param callback
 * @constructor
 */
function ActiveStatus(callback) {
    var wxinfo = getCookie('wxinfo');
    var wx_info = JSON.parse(wxinfo).row;
    var IP = wx_info.ctrlServer;
    var PORT = wx_info.ctrlPort;
    var TOKEN = wx_info.token;
    var active_url = config.active_task_status.replace('%s', IP).replace('%s', PORT).replace('%s', TOKEN);
    proxy(active_url, function (res) {
        var response = JSON.parse(res);
        callback(response);
    })
}


/**
 * getPaymentInfo
 */
function getPaymentInfo() {
    var wxinfo = getCookie('wxinfo');
    var wx_info = JSON.parse(wxinfo).row;
    var IP = wx_info.ctrlServer;
    var PORT = wx_info.ctrlPort;
    var TOKEN = wx_info.token;
    var active_url = config.active_task_status.replace('%s', IP).replace('%s', PORT).replace('%s', TOKEN);
}


/**
 * getDownload
 *
 */

function getDownload() {
    var wxinfo = getCookie('wxinfo');
    var wx_info = JSON.parse(wxinfo).row;
    var IP = wx_info.ctrlServer;
    var PORT = wx_info.ctrlPort;
    var TOKEN = wx_info.token;
    var down_url = config.all_download_status.replace('%s', IP).replace('%s', PORT).replace('%s', TOKEN);


    proxy(down_url, function (res) {
        var response = JSON.parse(res);
        var list = response.data.list;
        var html = '';
        for (var i = 0; i < list.length; i++) {
            var listDown = list[i];
            html += getDownloadList(listDown)
        }

        document.getElementById('downloadList').innerHTML = html;
    });
}


/**
 * 计算进度条
 * @param result
 * @param full
 * @param complete
 * @param total
 * @returns {number}
 */
function progressIng(result, full, complete, total){

    var full = parseInt(full);
    var complete = parseInt(complete);
    var total = parseInt(total);

    var result = Math.floor((complete/total)* full);

    return document.getElementById('result').style.width = result;

    setTimeout("progressIng()", 1000)

}


function getDownloadList(listDown) {
    var total = Math.round(listDown.filesize / 1024 / 1024);


    try {
        var progress = listDown.progress;
        var speed = Math.round(listDown.speed / 1024 / 1024);

        var complete = Math.ceil((progress.complete / progress.total) * 100);
        var completeSize = Math.round(((progress.complete / progress.total) * listDown.filesize) / 1024 / 1024);

        //回调进度条
        var a = parseInt(complete), b = parseInt(total);
        var full = document.getElementById('proFull').offsetWidth;
        console.log(a, b, full)
        progressIng(result, full, a, b)


    } catch (e) {
        var speed = complete = completeSize = 0;
    }



    return html = '<div class="row">'
        + '<div class="col-lg-4 col-sm-4 col-xs-4 video-nopadr">'
        + '<div class="full-content relative video-d-img">'
        + '<img src="' + listDown.pic + '"/>'
        + '</div>'
        + '</div>'

        + '<div class="col-lg-8 col-sm-8 col-xs-8">'
        + '<div class="full-content relative video-d-info">'
        + '<h5 class="video-d-name">' + listDown.videoname + '</h5>'
        + '<ul>'
        + '<li>' + complete + '%</li>'
        + '<li>' + completeSize + 'MB / ' + total + 'MB<span> ' + speed + 'm/s</span></li>'
        + '</ul>'
        + '<div class="pro-box relative">'
        + '<div id="proFull" class="proFull"></div>'
        + '<div id="result" class="result"></div>'
        + '</div>'
        + '</div>'
        + '</div>'
        + '</div>'

        + '<div class="row pd5">'
        + '<div class="col-lg-12 col-sm-12 col-xs-12">'
        + '<div class="full-content bottom-bottom1"></div>'
        + '</div>'
        + '</div>';
}








